package cn.tsvtc.shop.controller;

import cn.tsvtc.shop.entity.LoginUser;
import cn.tsvtc.shop.entity.ResultMessage;
import cn.tsvtc.shop.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author: Athena
 * @date: 2020/11/4 21:24
 * @description:
 */
@Controller
@RequestMapping("page")
public class PageController {

    private CartService cartService;

    @Autowired
    public PageController(CartService cartService){
        this.cartService=cartService;
    }

    @RequestMapping("cart")
    public String cart(Model model,@CookieValue(value = "cart_token",required = false) String cart_token,
                       @CookieValue(name = "login_token", required = false) String login_token){
        if (!StringUtils.isEmpty(login_token)) {
            LoginUser userByLoginToken = cartService.getUserByLoginToken(login_token);
            ResultMessage message =null;
            if(userByLoginToken!=null){
                //有对应的用户
                message = cartService.show(userByLoginToken.getId().toString());
            }else{
                message = cartService.show(login_token);
            }
            model.addAttribute("carts",message.getData());
        }else if(!StringUtils.isEmpty(cart_token)){
            ResultMessage message = cartService.show(cart_token);
            model.addAttribute("carts",message.getData());
        }else{
            //空token

        }
        return "shopcart.html";
    }
}
